from dash import Dash, dcc, html, dash_table, callback, Input, Output, State
from src.core.subject_recommendations.subject_recommendations import subject_recommendations

def create_subject_layout():
    return html.Div(
        children=[
            html.Header([
                html.H1('📚 科目选择推荐', 
                       style={'color': '#2c3e50', 'marginBottom': '20px'}),
                html.Div([
                    dcc.Input(
                        id='student-id-input',
                        type='text',
                        placeholder='输入学生ID查询推荐科目...',
                        debounce=True,
                        style={
                            'flex': 1,
                            'padding': '12px',
                            'border': '2px solid #3498db',
                            'borderRadius': '8px',
                            'fontSize': '16px'
                        }
                    ),
                    html.Button(
                        '查询 🔍',
                        id='submit-button',
                        n_clicks=0,
                        style={
                            'marginLeft': '10px',
                            'padding': '12px 24px',
                            'backgroundColor': '#3498db',
                            'color': 'white',
                            'border': 'none',
                            'borderRadius': '8px',
                            'cursor': 'pointer'
                        }
                    )
                ], style={'display': 'flex', 'maxWidth': '600px', 'margin': '0 auto'})
            ], style={'padding': '2rem', 'backgroundColor': '#f8f9fa'}),

            html.Main([
                dcc.Loading(
                    id="loading",
                    type="circle",
                    children=html.Div([
                        dash_table.DataTable(
                            id='recommendation-table',
                            columns=[
                                {"name": "学生ID", "id": "学生ID"},
                                {"name": "推荐科目1", "id": "推荐科目1"},
                                {"name": "推荐科目2", "id": "推荐科目2"},
                                {"name": "推荐科目3", "id": "推荐科目3"}
                            ],
                            page_current=0,
                            page_size=15,
                            page_action='custom',
                            style_table={
                                'borderRadius': '8px',
                                'overflowX': 'auto',
                                'boxShadow': '0 2px 4px rgba(0,0,0,0.05)'
                            },
                            style_header={
                                'backgroundColor': '#3498db',
                                'color': 'white',
                                'fontWeight': 'bold'
                            },
                            style_cell={
                                'minWidth': '120px',
                                'padding': '12px',
                                'fontFamily': 'Segoe UI',
                                'textAlign': 'center',
                                'whiteSpace': 'normal'
                            },
                            style_data_conditional=[
                                {'if': {'row_index': 'odd'}, 
                                 'backgroundColor': '#f8f9fa'}
                            ]
                        )
                    ], style={
                        'maxWidth': '1200px',
                        'margin': '20px auto',
                        'padding': '20px',
                        'backgroundColor': 'white',
                        'borderRadius': '8px'
                    })
                )
            ], style={'padding': '2rem 0'})
        ],
        style={
            'fontFamily': 'Segoe UI, system-ui',
            'minHeight': '100vh',
            'backgroundColor': '#ecf0f1'
        }
    )

def register_subject_callbacks(app):
    @app.callback(
        Output('recommendation-table', 'data'),
        Output('recommendation-table', 'page_count'),
        Input('submit-button', 'n_clicks'),
        Input('recommendation-table', 'page_current'),
        Input('recommendation-table', 'page_size'),
        State('student-id-input', 'value')
    )
    def update_table(n_clicks, page_current, page_size, input_id):
        df = subject_recommendations()
        
        if input_id:
            search_id = str(input_id).strip()
            filtered_df = df[df['学生ID'] == search_id]
        else:
            filtered_df = df.copy()
        
        filtered_data = filtered_df.to_dict('records')
        page_count = max(1, len(filtered_data) // page_size + (1 if len(filtered_data) % page_size else 0))
        start = page_current * page_size
        end = start + page_size
        
        return filtered_data[start:end], page_count

def init_subject_recommendations(server):
    app = Dash(
        __name__,
        server=server,
        url_base_pathname='/subject_recommendations/'
    )
    
    app.layout = create_subject_layout()
    register_subject_callbacks(app)
    
    return app
